﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using Hotel.Classes.Helper;
using Hotel.Models.Entity;
using Hotel.Models.Services;

namespace Hotel.Models.DataAccess
{
    public class DARoom
    {
        public static List<Room> GetListRoom(int roomID)
        {
            DataTable dt;
            List<Room> list = new List<Room>();
            using (DBManager dbManager = new DBManager("stp_Get_Room"))
            {
                dbManager.Add("p_PK_RoomID", roomID);
                dbManager.Add("IsDelete", 0);
                dt = dbManager.GetDatatable();
            }
            foreach (DataRow dr in dt.Rows)
            {
                Room room = new Room();
                room.PkRoomId = DataHelper.GetInt(dr, "PK_RoomID");

                Floor floor = new Floor();
                floor.PkFloorId = DataHelper.GetInt(dr, "FK_FloorID");
                room.Floor = floor;
                RoomType rt = RoomTypeService.GetRoomTypeDetail(DataHelper.GetInt(dr, "FK_RoomTypeId"));
                room.RoomType = rt;
                room.Name = DataHelper.Get(dr, "Name");
                room.Amenities = DataHelper.Get(dr, "Amenities");
                room.Information = DataHelper.Get(dr, "Information");
                room.NoBeds = DataHelper.GetInt(dr, "NoBeds");
                room.NoPerson = DataHelper.GetInt(dr, "NoPerson");
                list.Add(room);
            }
            return list;
        }

        public static bool DeleteRoom(int roomID)
        {
            using (DBManager dbManager = new DBManager("stp_Get_Room"))
            {
                try
                {
                    dbManager.Add("p_PK_RoomID", roomID);
                    dbManager.Add("IsDelete", 1);
                    dbManager.ExecuteNonQuery();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

        public static bool SaveRoom(Room room, bool isUpdate)
        {
            Floor floor = room.Floor;
            using (DBManager dbManager = new DBManager("stp_Create_Room"))
            {
                try
                {
                    dbManager.Add("p_PK_RoomID", room.PkRoomId);
                    dbManager.Add("p_FK_FloorID", floor.PkFloorId);
                    dbManager.Add("p_FK_RoomTypeId", room.RoomType.RoomTypeId);
                    dbManager.Add("p_Name", room.Name);
                    dbManager.Add("p_Amenities", room.Amenities);
                    dbManager.Add("p_Information", room.Information);
                    dbManager.Add("p_NoBeds", room.NoBeds);
                    dbManager.Add("p_NoPerson", room.NoPerson);
                    dbManager.Add("IsUpdate", isUpdate);
                    dbManager.ExecuteNonQuery();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
    }
}